package tw.com.msig.jaxb;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Jaxb01App {

	public static void main(String[] args) {

		Policy policy = new Policy();
		policy.setId(new Long(1));
		policy.setPolicyNo("CAR3014070502");
		
		List<Insured> insureds = new ArrayList<Insured>();
		
		Insured insured1 = new Insured();
		insured1.setId(new Long(1));
		insured1.setIdNo("A123456789");
		insured1.setName("Lion");
		
		Insured insured2 = new Insured();
		insured2.setId(new Long(2));
		insured2.setIdNo("A223732851");
		insured2.setName("Gary");	
		
		insureds.add(insured1);
		insureds.add(insured2);
		
		policy.setInsureds(insureds);
		
		System.out.println("policy1:" + policy);
		
		try {
			JAXBContext jc = JAXBContext.newInstance(Policy.class);
			
			// write xml
			Marshaller marshaller = jc.createMarshaller();
			File file = new File("C:\\temp\\sample03.xml");
			marshaller.marshal(policy, file);
			
			// read from xml
			Unmarshaller unmarshaller = jc.createUnmarshaller();
			Policy policy2 = (Policy) unmarshaller.unmarshal(file);
			
			System.out.println("policy2:" + policy2);
			
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		
	}

}
